﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using CandleStickChart.Stock;
using System.ComponentModel;
using System.Windows.Threading;
using FXSStation.Modules.ChartModule.ChartData;
using System.IO;
using FXSStation.Modules.SharedInfrastructureModule.BusinessClasses.Chart;
namespace CandleStickChart.Chart_Types
{
    /// <summary>
    /// Interaction logic for CandleChart.xaml
    /// </summary>
    public partial class CandleChart : UserControl,INotifyPropertyChanged
    {
        private ChartStyle cs = new ChartStyle();
        private DispatcherTimer timer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(1) };
        public ChartStyle ChartStyle
        {
            get { return cs; }
            set { cs = value; RaisePropertyChanged("ChartStyle"); }
        }
        private DataCollection dc = new DataCollection();
        ObservableCollection<StockItem> items = new ObservableCollection<StockItem>();
        public DataCollection DataCollection
        {
            get { return dc; }
            set { dc = value; RaisePropertyChanged("DataCollection"); }
        }
        private DataSeries ds = new DataSeries();
        int i = 0;
        public DataSeries DataSeries
        {
            get { return ds; }
            set { ds = value; RaisePropertyChanged("DataSeries"); }
        }
        public CandleChart()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(CandleChart_Loaded);
            timer.Tick += new EventHandler(timer_Tick);
        }

        void timer_Tick(object sender, EventArgs e)
        {
            if (this.DataSeries.DataItems.Count < 1)
            {
                this.DataSeries.DataItems.Add(items[0]);
                this.DataSeries.DataItems.Add(items[1]);
                i = 2;
            }
            InsertItem(items[i]);
            i++;
        }

        void CandleChart_Loaded(object sender, RoutedEventArgs e)
        {
            dc = new DataCollection();
            cs = new ChartStyle();
            
        }

        private void chartGrid_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            textCanvas.Width = chartGrid.ActualWidth;
            textCanvas.Height = chartGrid.ActualHeight;
            chartCanvas.Children.Clear();
            textCanvas.Children.RemoveRange(1, textCanvas.Children.Count - 1);

            AddChart();
        }
        private void LoadFile_Click(object sender, RoutedEventArgs e)
        {
            dc.DataList.Clear();
            ds = new CandleStickChart.Stock.DataSeries();
            items = CacheData();
            ds.DataItems = items;
            ds.LineColor = Brushes.DarkBlue;
            ds.FillColor = Brushes.DarkBlue;
            dc.DataList.Add(ds);
            //timer.Start();
            AddChart();
        }
        private ObservableCollection<StockItem> CacheData()
        {
            ImportService service = new ImportService();
            FileInfo file = new FileInfo(@"..\..\..\Modules\ChartData\EURUSD\60min\EURUSD60.csv");
            if (!file.Exists)
                return null;
            else
            {
                service.LoadData(file.FullName);
                ObservableCollection<StockItem> data = service._row_chart_data;
                return data;
            }
        }
        public void InsertItem(StockItem item)
        {
            textCanvas.Width = chartGrid.ActualWidth;
            textCanvas.Height = chartGrid.ActualHeight;
            chartCanvas.Children.Clear();
            textCanvas.Children.RemoveRange(1, textCanvas.Children.Count - 1);
            if (dc.DataList.Count > 0)
            {
                cs = new ChartStyle();
                cs.ChartCanvas = chartCanvas;
                cs.TextCanvas = textCanvas;
                cs.XMin = -1;
                cs.XMax = ds.DataItems.Count;
                cs.XTick = 3;
                cs.YMin = double.Parse(txYmin.Text);
                cs.YMax = double.Parse(txYmax.Text);
                cs.YTick = double.Parse(txYTick.Text);
                cs.Title = "Stock Chart";
                cs.AddChartStyle(tbTitle, tbXLabel, tbYLabel, ds);
                dc.StockChartType = DataCollection.StockChartTypeEnum.Candle;
                dc.AddNewPoint(cs, ds,item);

            }
        }
        public void AddChart()
        {
            if (DataCollection.DataList.Count > 0)
            {
                ChartStyle = new ChartStyle();
                ChartStyle.ChartCanvas = chartCanvas;
                ChartStyle.TextCanvas = textCanvas;
                ChartStyle.XMin = -1;
                ChartStyle.XMax = ds.DataItems.Count;
                ChartStyle.XTick = 3;
                ChartStyle.YMin = double.Parse(txYmin.Text);
                ChartStyle.YMax = double.Parse(txYmax.Text);
                ChartStyle.YTick = double.Parse(txYTick.Text);
                ChartStyle.Title = "Stock Chart";
                ChartStyle.AddChartStyle(tbTitle, tbXLabel, tbYLabel, ds);
                DataCollection.StockChartType = CandleStickChart.Stock.DataCollection.StockChartTypeEnum.Candle;
                DataCollection.AddStockChart(cs);
            }
        }

        #region INotifyPropertyChanged values

        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

    }
}
